package com.cy.ppj.service.account.impl;

import com.cy.ppj.comm.constant.CacheConstants;
import com.cy.ppj.comm.utils.CommonUtils;
import com.cy.ppj.dao.mapper.account.TeamCommissionMapper;
import com.cy.ppj.dao.mapper.user.UserRecommenderMapper;
import com.cy.ppj.model.PageBean;
import com.cy.ppj.model.enums.account.BrokerLevel;
import com.cy.ppj.model.query.account.TeamMemberQuery;
import com.cy.ppj.model.vo.admin.teamcommission.TeamMemberAdminVO;
import com.cy.ppj.model.vo.admin.teamcommission.TeamMemberCountStatisticsVO;
import com.cy.ppj.model.vo.front.account.MyTeamStatisticsVO;
import com.cy.ppj.model.vo.front.account.MyTeamVO;
import com.cy.ppj.model.vo.front.account.TeamMemberVO;
import com.cy.ppj.service.BaseService;
import com.cy.ppj.service.account.TeamCommissionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @description: 团队佣金
 * @author: Ocean
 * @create: 2018-09-06 23:35
 **/
@Slf4j
@Service
public class TeamCommissionServiceImpl extends BaseService implements TeamCommissionService {

    @Autowired
    private TeamCommissionMapper teamCommissionMapper;
    @Autowired
    private UserRecommenderMapper userRecommenderMapper;

    @Override
    public PageBean<TeamMemberVO> queryTeamMembers(TeamMemberQuery query) {

        query.setLevel(null);
        List<TeamMemberVO> list = new ArrayList<>();
        int total = teamCommissionMapper.selectTeamMemberCount(query);
        if (total > 0) {
            query.startPage();
            list = teamCommissionMapper.selectTeamMembers(query);
//            list.stream().filter(t -> StringUtils.hasLength(t.getRealName())).forEach(t -> {
//                t.setRealName(CommonUtils.nameFormat(t.getRealName()));
//            });
            list.stream().forEach(t -> {
                if(!CommonUtils.isEmpty(t.getRealName())) {
                    t.setRealName(CommonUtils.nameFormat(t.getRealName()));
                }
                if(t.getLevel() == 1) {
                    t.setRecommendMobile(null);
                    t.setRecommendRealName(null);
                }else if(t.getLevel() == 2) {
                    if(!CommonUtils.isEmpty(t.getRecommendRealName())) {
                        t.setRecommendRealName(CommonUtils.nameFormat(t.getRecommendRealName()));
                    }
                }
                if(!CommonUtils.isEmpty(t.getBrokerLevel())) {
                    t.setBrokerLevelText(t.getBrokerLevel().getText());
                }else {
                    t.setBrokerLevelText("未实名");
                }
            });
        }

        return new PageBean<>(list, total);
    }

    @Override
    public MyTeamVO queryMyTeamInfo(Long userId) {

        MyTeamVO myTeamVO = teamCommissionMapper.selectMyTeamInfo(userId);
//        if (myTeamVO != null) {
//            myTeamVO.setTaemCount(myTeamVO.getTaemFirstCount() + myTeamVO.getTaemSecondCount());
//        }
        return myTeamVO;
    }

    @Override
    public MyTeamVO queryMyTeamInfo2(Long userId) {

        MyTeamVO myTeamVO = teamCommissionMapper.selectMyTeamInfo2(userId);
        if(myTeamVO != null) {
            int taemAllCount = userRecommenderMapper.selectCountByRecommendUserIdAndLevel(userId, null);
            int diamondCount = userRecommenderMapper.selectCountByRecommendUserIdAndLevel(userId, BrokerLevel.DIAMOND);
            int goldenCount = userRecommenderMapper.selectCountByRecommendUserIdAndLevel(userId, BrokerLevel.GOLDEN);
            int officialCount = userRecommenderMapper.selectCountByRecommendUserIdAndLevel(userId, BrokerLevel.OFFICIAL);
            int unRealAuthCount = taemAllCount - diamondCount - goldenCount - officialCount;
            myTeamVO.setTaemAllCount(taemAllCount);
            myTeamVO.setDiamondCount(diamondCount);
            myTeamVO.setGoldenCount(goldenCount);
            myTeamVO.setOfficialCount(officialCount);
            myTeamVO.setUnRealAuthCount(unRealAuthCount);
        }

        return myTeamVO;
    }

    @Override
    @Cacheable(value = CacheConstants.CACHE_VALUE_1H)
    public MyTeamStatisticsVO queryMyTeamStatistics(Long userId, Integer type) {

        Date time = new Date();
        Date startTime = null;
        Date endTime = null;

        switch (type) {
            case 1:
                //七天
                startTime = CommonUtils.addTime(time,-7, Calendar.DAY_OF_YEAR);
                endTime = CommonUtils.getDayEndTime(time);
                break;

            case 2:
                //一个月
                startTime = CommonUtils.addTime(time, -1, Calendar.MONTH);
                endTime = CommonUtils.getDayEndTime(time);
                break;

            case 3:
                startTime = CommonUtils.addTime(time, -3, Calendar.MONTH);
                endTime = CommonUtils.getDayEndTime(time);
                break;


            default:
                state(false,"查询条件异常");
        }

        return teamCommissionMapper.selectMyTeamStatistics(userId, startTime, endTime);
    }

	@Override
	public PageBean<TeamMemberAdminVO> selectTeamMemberAdmin(TeamMemberQuery query) {
		int total = teamCommissionMapper.selectTeamMemberAdminCount(query);
		List<TeamMemberAdminVO> list = new ArrayList<>();
		if(total > 0) {
			query.startPage();
			list = teamCommissionMapper.selectTeamMemberAdminList(query);
			list.stream().forEach(u -> {
				if(1 == u.getLevel()) {
					u.setLevelText("一级成员");
				}else if(2 == u.getLevel()) {
					u.setLevelText("二级成员");
				}
			});
		}
		return new PageBean<>(list, total);
	}

	@Override
	public TeamMemberCountStatisticsVO myTeamMembersCountStatistics(Long userId) {
		TeamMemberCountStatisticsVO vo = teamCommissionMapper.selectMyTeamMembersCountStatistics(userId);
		return vo;
	}
}
